//
//  ListTableView.swift
//  fruit-helper-swift
//
//  Created by 赵隆杰 on 2022/6/25.
//

import UIKit
typealias backEnveListTableClosureType = () -> Void//定义闭包类型特定的函数类型
class ListTableView: UITableView {
    
//    fileprivate lazy var list: [BookModel] = { () -> [BookModel] in
//        let list = []
//        return list
//    }()
    
    
    var modelList_: [BookModel]? {
        didSet {
//            modelList_ = list
            reloadData()
        }
    }
    
    var tableHeaderReloadBlock : backEnveListTableClosureType?

    var tableCanScroll = false
    
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        
        tableCanScroll = false
        
        contentInsetAdjustmentBehavior = .never
        backgroundColor = .clear
        
        rowHeight = 150.0
        estimatedRowHeight = 150.0
        estimatedSectionHeaderHeight = 0.0
        estimatedSectionFooterHeight = 0.0
        tableFooterView = UIView.init(frame: CGRect(x: 0, y: 0, width: SL_WINDOW_WIDTH, height: CGFloat.leastNormalMagnitude + 5))
        delegate = self
        dataSource = self
        separatorStyle = .none
        
        register(ListTableViewCell.self, forCellReuseIdentifier: "ListTableViewCell")
        
        NotificationCenter.default.addObserver(self, selector: #selector(scrollNotification(_:)), name: NSNotification.Name(rawValue: "List_tableCanScroll"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(scrollHeaderRefreshStop(_:)), name: NSNotification.Name(rawValue: "List_tableHeaderRefresh"), object: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "List_tableCanScroll"), object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "List_tableHeaderRefresh"), object: nil)
    }
    
}

extension ListTableView: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return modelList_?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: ListTableViewCell = self.dequeueReusableCell(withIdentifier: "ListTableViewCell", for: indexPath) as! ListTableViewCell
        cell.model = modelList_?[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        guard let model = modelList_?[indexPath.row] else {return}
        let vc = BookDetailViewController()
        vc.model = model
        vc.backEnve = { () -> Void in
            
        }
        UIWindow().currentViewController()!.navigationController?.pushViewController(vc, animated: true)
        
       
    }
    
    
}

extension ListTableView {
    @objc func scrollNotification(_ notification: Notification) {
        let userInfo: NSDictionary = notification.userInfo! as NSDictionary
        let canScroll: String = userInfo["canScroll"] as! String
        if (canScroll as NSString).boolValue {
            tableCanScroll = true
        }
    }
    
    @objc func scrollHeaderRefreshStop(_ notification: Notification) {
        mj_header?.endRefreshing()
    }
    
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
    
    
    
}
